﻿using Coldairarrow.Util;
using NetTopologySuite.Index.Bintree;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace Coldairarrow.Business.Interface
{
    

    /// <summary>
    /// 过滤类
    /// </summary>
    public class ComparerFromKey<T> : IEqualityComparer<T>
    {

        /// <summary>
        /// 需要比较的过滤键
        /// </summary>
        public string _key;

        /// <summary>
        /// 需要更新的行字段名称
        /// </summary>
        public string _rowId;
        public ComparerFromKey(string key, string rowId)
        {
            _key = key;
            _rowId = rowId;
        }
        public bool Equals(T x1, T x2)
        {
            if (ReferenceEquals(x1, x2))
                return true;
            if (x1 is null || x2 is null)
                return false;
            
            string FID1= x1.GetPropertyValue(_key).IsNullOrEmpty()?"" : x1.GetPropertyValue(_key).ToString();
            string FID2= x2.GetPropertyValue(_key).IsNullOrEmpty() ? "" : x2.GetPropertyValue(_key).ToString();
            //更新时将目标的rowId复制给源的rowId
            if (FID1 == FID2 && x2.GetPropertyValue(_rowId).IsNullOrEmpty())
            {
                x2.SetPropertyValue(_rowId, x1.GetPropertyValue(_rowId));
            }
            return FID1 == FID2;
        }

        public int GetHashCode(T obj)
        {
            return obj.GetPropertyValue(_key).ToString().GetHashCode();
        } 
    }
}
